package day03;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Demo01 {
    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(ArrayList.class);
        enhancer.setCallback(
                new ProxyInterceptor<String>(new ArrayList<String>()));
        ArrayList<String> list = (ArrayList<String>) enhancer.create();
        System.out.println(list.getClass());
        list.add("Tom");
        list.add("Jerry");
        list.add("Andy");
        System.out.println(list.size());
        System.out.println(list);
    }
}
class ProxyInterceptor<E> implements MethodInterceptor{
    final private List<E> target;
    public ProxyInterceptor(List<E> list){
        target = list;
    }
    public Object intercept(
            Object proxy,
            Method method,
            Object[] args,
            MethodProxy methodProxy) throws Throwable {
        System.out.println("ProxyInterceptor.intercept");
        synchronized (target){
            return method.invoke(target, args);
        }
    }
}